package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldIncharge;
import edu.zqu.collegeField.project.mapper.FieldInchargeMapper;
import edu.zqu.collegeField.project.service.FieldInchargeService;
import edu.zqu.collegeField.project.vo.FieldInchargeVo;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地负责人 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldInchargeServiceImpl extends ServiceImpl<FieldInchargeMapper, FieldIncharge> implements FieldInchargeService {
    @Autowired
    private FieldInchargeMapper fieldInchargeMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;

    @Override
    public List<FieldInchargeVo> get(BigInteger collegeId, BigInteger fieldId, BigInteger workerId) {//获取场地负责人列表
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("fieldId", Convert.toStr(fieldId));
        map.put("workerId", Convert.toStr(workerId));
        List<FieldInchargeVo> fieldInchargeVoList = BeanMapper.mapList(fieldInchargeMapper.get(map), FieldInchargeVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldInchargeVo fieldInchargeVo : fieldInchargeVoList) {
            fieldInchargeVo.setCollegeName(collegeName);
        }
        return fieldInchargeVoList;
    }

    @Override
    public FieldInchargeVo getById(BigInteger id) {//获取场地负责人
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldInchargeVo fieldInchargeVo = BeanMapper.map(fieldInchargeMapper.getById(map), FieldInchargeVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldInchargeVo.getCollegeId());
        fieldInchargeVo.setCollegeName(collegeName);
        return fieldInchargeVo;
    }

    @Override
    public int add(FieldIncharge fieldIncharge) {//添加场地负责人
        fieldIncharge.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldInchargeMapper.insert(fieldIncharge);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldIncharge fieldInchargeUpdate) {//修改场地负责人
        FieldIncharge fieldIncharge = fieldInchargeMapper.selectById(fieldInchargeUpdate.getId());
        if (StringUtils.isNull(fieldIncharge)) {
            throw new NullPointerException("找不到该场地负责人");
        }
        BeanMapper.copyPropertiesNotNull(fieldInchargeUpdate, fieldIncharge);
        try {
            int insert = fieldInchargeMapper.insert(fieldIncharge.setLastVersion(fieldInchargeUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldInchargeUpdate.getId(), fieldInchargeUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地负责人
        try {
            UpdateWrapper<FieldIncharge> fieldInchargeUpdateWrapper = new UpdateWrapper<FieldIncharge>().eq("id", Convert.toStr(id));
            int update = fieldInchargeMapper.update(new FieldIncharge().setByWho(byWho), fieldInchargeUpdateWrapper);
            if (update >= 1) {
                return fieldInchargeMapper.delete(fieldInchargeUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
